package com.quik.backend.service.account;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.quik.backend.mapper.BaseDao;
import com.quik.backend.mapper.SysAccountDao;
import com.quik.backend.service.BaseServiceImpl;
import com.quik.entity.SysAccount;
import com.quik.entity.SysRoleRight;
import com.quik.util.CommonUtils;

@Service
public class AcServiceImpl extends BaseServiceImpl implements AcService {

	@Autowired
	private SysAccountDao dao;

	public BaseDao getDao(){
		return dao;
	}
	
	@Override
	public SysAccount login(String loginName, String password) throws Exception {
		if (loginName == null || loginName.trim().length() <= 0){
			throw new RuntimeException("请输入工号.");
		}

		if (password == null || password.trim().length() <= 0){
			throw new RuntimeException("请输入密码.");
		}

		SysAccount account = (SysAccount) dao.queryOneByProp(SysAccount.class,
				"staff_no", loginName.trim());

		if (account == null) throw new RuntimeException("工号或者密码不正确.");
		boolean isValided = CommonUtils.validatePassword(password,
				account.getLogin_password());
		if (isValided)
			return account;
		throw new RuntimeException("工号或者密码不正确.");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysRoleRight> findRoleRightByUser(SysAccount account) {
		return dao.load("findSysRoleRightByUserId", account.getId());
	}
}
